package com.maikang.exammall.configuration;

import com.maikang.exammall.interceptor.AuthInterceptor;
import com.maikang.exammall.interceptor.MobileRequiredInterceptor;
import com.maikang.exammall.repository.UserRepository;
import com.maikang.exammall.service.RedisService;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@AllArgsConstructor
//public class WebMvcConfig extends WebMvcConfigurationSupport {
public class WebMvcConfig implements WebMvcConfigurer {
    private final RedisService redisService;
    private final UserRepository userRepository;
    /*
     * 拦截器配置
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册自定义拦截器，添加拦截路径和排除拦截路径
        registry.addInterceptor(new AuthInterceptor(redisService, userRepository)) // 添加拦截器
                .addPathPatterns("/api/**") // 添加拦截路径
                .excludePathPatterns(// 添加排除拦截路径
                        "/swagger*",
                        "/swagger-ur.html",
                        "/swagger-ur.html/**",
                        "/wx/**",
                        "/ma/**",
                        "/api/diseases/**",
                        "/api/categories/**",
                        "/api/health",
                        "/api/kefu/**",
                        "/api/users/profile/earning-spec").order(0);//执行顺序


        // 注册自定义拦截器，添加拦截路径和排除拦截路径
        registry.addInterceptor(new MobileRequiredInterceptor()) // 添加拦截器
                .addPathPatterns(
                        "/api/users/**",
                        "/api/cart/**",
                        "/api/orders/**") // 添加拦截路径
                .excludePathPatterns(// 添加排除拦截路径
                        "/api/users/position",
                        "/api/users/login",
                        "/api/users/login-captcha",
                        "/api/users/login-sms",
                        "/swagger*",
                        "/swagger-ur.html/**",
                        "/swagger-ur.html",
                        "/wx/**",
                        "/api/diseases/**",
                        "/api/categories/**",
                        "/api/health",
                        "/api/kefu/**",
                        "/api/users/profile/earning-spec").order(1);//执行顺序
//       super.addInterceptors(registry);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

//        registry.addResourceHandler("/**").addResourceLocations("classpath:/resources/"); this is done by calling super

//        super.addResourceHandlers(registry);
    }
}